package com.hs.biye.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hs.biye.constant.MessageConstant;
import com.hs.biye.excepction.BaseException;
import com.hs.biye.model.DTO.DepartmentRequest;
import com.hs.biye.model.VO.DepartmentVo;
import com.hs.biye.model.entity.Department;
import com.hs.biye.model.entity.Employee;
import com.hs.biye.model.reslut.Result;
import com.hs.biye.model.reslut.ResultPage;
import com.hs.biye.service.DepartmentService;
import com.hs.biye.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/depart")
public class DepartmentController {
    
    @Resource
    private DepartmentService departmentService;


    /**
     * 获取单个员工信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getone/{id}")
    public Result<DepartmentVo> getDepartmentOne(@PathVariable Integer id) {
        if (id == null) {
            return Result.faile(MessageConstant.PAR_NOT_RIGHT);
        }
        DepartmentVo departmentVo = departmentService.getOneDepartment(id);
        if (departmentVo == null) {
            return Result.faile(MessageConstant.NOT_QUERY);
        }
        return Result.success(departmentVo);
    }


    /**
     * 分页查询
     * @param departmentRequest
     * @return
     */
    @PostMapping("/getpages")
    public ResultPage<DepartmentVo> getDepartment(@RequestBody DepartmentRequest departmentRequest) {
        if (departmentRequest == null) {
            return ResultPage.faile(MessageConstant.PAR_NOT_RIGHT);
        }
        ResultPage<DepartmentVo> department = departmentService
                .getDepartment(departmentRequest.getPages(), departmentRequest.getSize());
        if (department == null) {
            return ResultPage.faile(MessageConstant.NOT_QUERY);
        }
        return department;
    }





    @GetMapping("/getNameId")
    public Result<List<DepartmentVo>> getDepartmentNameAndId(){
        List<DepartmentVo> departmentNameAndId = departmentService.getDepartmentNameAndId();
        return Result.success(departmentNameAndId);
    }




    /**
     * 添加员工
     * @param departmentRequest
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> addDepartment(@RequestBody DepartmentRequest departmentRequest) {
        if (departmentRequest == null) {
            return Result.faile(MessageConstant.PAR_NOT_RIGHT);
        }
        boolean b = departmentService.addDepartment(departmentRequest);
        return Result.success(b);
    }


    @PostMapping("/update")
    public Result<Boolean> updateDepartment(@RequestBody DepartmentRequest departmentRequest) {
        if (departmentRequest == null) {
            return Result.faile(MessageConstant.PAR_NOT_RIGHT);
        }
        boolean b = departmentService.updateDepartment(departmentRequest);
        return Result.success(b);
    }




    @PostMapping("/delete/{id}")
    public Result<Boolean> deleteDepartment(@PathVariable Integer id) {
        if (id == null) {
            return Result.faile(MessageConstant.PAR_NOT_RIGHT);
        }
        boolean b = departmentService.removeDepartment(id);
        return Result.success(b);
    }


}
